// jqgrid表格配置
    // 使jqgrid的宽和高自适应父元素
    $(function(){
        //页面加载完成之后执行
        pageInit();
    });

    // 初始化jqGrid
    // 状态显示
    function state(state){
        if(state == 0){
            return '未分派';
        }else if(state == 1) {
            return '进行中';
        }else if(state == 2) {
            return '完成';
        }
    }
    // 操作按钮
    function check() {
        var check = "";
            check = "<a href='#qualitycontrol-workOrderMaintenance-checkWorkOrder' class='btn btn-primary btn-sm check' title='查看'>查看</a>";
        return check;
    };
    function pageInit(){
        var parentHeight,parentWidth;
        parentHeight = $("#workOrder-table").height() - 23,
        parentWidth = $("#workOrder-table").width() + 14;
        //创建jqGrid组件
        jQuery("#workOrderList").jqGrid({
            url: contextPath + "/workorder/queryWorkOrderJqg",
            datatype: "json",
            height: parentHeight,
            width: parentWidth,
            colNames:['工单Id','工单名称','设备编号','设备类型Id','设备类型','施工单位Id','施工单位','项目名称','产品线','状态','进展','派单时间','完成时间','操作'],
            colModel:[
                    {name:'workOrderId',index:'workOrderId', sortable: false,hidden: true},
                    {name:'workOrderName',index:'workOrderName', sortable: false},
                    {name:'deviceId',index:'deviceId', sortable: false},
                    {name:'deviceTypeId',index:'deviceTypeId', sortable: false,hidden: true},
                    {name:'deviceTypeName',index:'deviceTypeName', sortable: false},
                    {name:'constructionTeamIds',index:'constructionTeamIds', sortable: false,hidden: true},
                    {name:'constructionTeamNames',index:'constructionTeamNames', sortable: false},
                    {name:'projectName',index:'projectName', sortable: false},
                    {name:'productLineName',index:'productLineName', sortable: false},
                    {name:'state',index:'state', sortable: false,formatter: state},
                    {name:'progress',index:'progress', sortable: false},
                    {name:'assignTime',index:'assignTime', sortable: false},
                    {name:'endTime',index:'endTime', sortable: false},
                    {name:'subTaskState',index:'subTaskState', sortable: false,formatter: check}
            ],
            sortname:'',
            sortorder:'desc',
            viewrecords: true,
            rowNum: 20,
            rowList:[20,50,100],
            pager:"#gridpager"
        });
        /*创建jqGrid的操作按钮容器*/
        jQuery("#workOrderList").jqGrid('navGrid', '#gridpager', {edit: false,add: false, del:false,search: false});
    };
    //函数：获取尺寸　　
    var timer = 0;
    $(window).resize(function(){
        clearTimeout(timer);
        timer = setTimeout(function() {
            $("#workOrderList").setGridWidth($('#workOrder-table').width() + 12);
            $("#workOrderList").setGridHeight($('#workOrder-table').height() - 23);
        }, 260);
    });　　
    // 侧边栏缩小时，实时的给jqgrid的宽和高赋值
    $("#asidebarSwitch").on('click',function() {
        var time = 0;
        var set = setInterval(function (){
            $("#workOrderList").setGridWidth($('#workOrder-table').width()*1 + 14);　　
            $("#workOrderList").setGridHeight($('#workOrder-table').height()*1 - 23);
            time++;
            if(time == 201){
                clearInterval(set);
            }
        },time);
    });

// 查询区域
    // 查询所有产品线
    $.ajax({
        url: contextPath + "/ProductLine/queryAllProductLine",
        type: 'GET',
        dataType: "json",
        contentType: "application/json",
        success: function(data){
            var str = '<option value="" selected>选择产品线</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#productLineId").html(str);
        },
        error: function(data){
            toastr.error(data.message, "请求失败");
        }
    });
    // 查询设备类型
    $.ajax({
        url: contextPath + "/DeviceType/queryAllDeviceType",
        type: 'GET',
        dataType: "json",
        contentType: "application/json",
        success: function(data){
            var str = '<option value="" selected>选择设备类型</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#deviceTypeId").html(str);
        },
        error: function(data){
            toastr.error(data.message, "请求失败");
        }
    });

    // 查询
    $('#query').on('click',function(){
        var productLineId = $("#productLineId").val();
        var projectName = $("#projectName").val();
        var constructionTeamName = $("#constructionTeamName").val();
        var deviceName = $("#deviceName").val();
        var deviceTypeId = $("#deviceTypeId").val();
        var workOrderState = $("#workOrderState").val();
        $("#workOrderList").setGridParam({
            url : contextPath + "/workorder/queryWorkOrderJqg",
            postData : {
                queryCond: {
                    productLineId: productLineId,
                    projectName: projectName,
                    constructionTeamName: constructionTeamName,
                    deviceName: deviceName,
                    deviceTypeId: deviceTypeId,
                    workOrderState: workOrderState
                }
            },
            page: 1
        }).trigger("reloadGrid");
    });

    function buildURL(url,param/*链接和参数*/){
        var paramStr = '';
        $.each(param,function(key,item){
            var link = '&' + key + "=" + item;
            paramStr += link;
        })
        paramStr = url + "?" + paramStr.substr(1);
        return paramStr.replace(' ','');
    }

    // 导出
    $('#export').on('click',function(){
        var productLineId = $("#productLineId").val();
        var projectName = $("#projectName").val();
        var constructionTeamName = $("#constructionTeamName").val();
        var deviceName = $("#deviceName").val();
        var deviceTypeId = $("#deviceTypeId").val();
        var workOrderState = $("#workOrderState").val();

        var obj = new Object();
        obj.productLineId = productLineId;
        obj.projectName = projectName;
        obj.constructionTeamName = constructionTeamName;
        obj.deviceName = deviceName;
        obj.deviceTypeId = deviceTypeId;
        obj.workOrderState = workOrderState;

        var url = buildURL("workorder/exportWorkOrder",obj);
        window.open(url);
    });

    // 查看
    // 获取工单数据
    $("#workOrderList").on('click','.check',function(){
        var id = $(this).parent().parent().attr('id');
        var workOrderData = $("#workOrderList").jqGrid('getRowData',id);
        console.log(workOrderData.workOrderId);
        $.cookie('workOrderId',JSON.stringify(workOrderData.workOrderId));
    });